﻿using Microsoft.EntityFrameworkCore;
using SmartParking.Entities;
using SmartParking.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartParking.Service
{
    public class MenuService : BaseService, IMenuService
    {
        public MenuService(DbContext dbContext)
            : base(dbContext)
        {

        }

        public IEnumerable<SysMenu> GetMenuList(string key)
        {
            return this.Query<SysMenu>(t => string.IsNullOrWhiteSpace(key)
                                            || t.MenuHeader.Contains(key)
                                            || t.TargetView.Contains(key)
                                            || Context.Set<SysMenu>().Where(p => p.ParentId == t.Id
                                                                                    && (p.MenuHeader.Contains(key)
                                                                                    || p.TargetView.Contains(key))).Count() > 0);
        }

        public int GetLastId()
        {
            var tmp = this.Query<SysMenu>(t => 1 == 1).OrderByDescending(o => o.Id).ToList();
            
            if (tmp.Count > 0 && tmp.FirstOrDefault() != null)
            {
                return tmp.FirstOrDefault().Id;
            }
            else
            {
                return 0;
            }
        }

        public int GetMenuCount() 
        {
            return this.Query<SysMenu>(t => 1 == 1).Count();
        }
    }
}
